package com.jianshuge.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jianshuge.app.AppContext;
import com.jianshuge.app.AppException;
import com.jianshuge.app.R;
import com.jianshuge.app.bean.MyResult;

import com.jianshuge.app.common.UIHelper;
import com.jianshuge.app.widget.LinkView;

public class BookCommentDetail extends BaseActivity {
	
	private ImageView mBack;
	private ImageView mRefresh;
	private ProgressBar mProgressbar;	
	private LinearLayout mLinearlayout;
	
	private TextView comment_title;
	private LinkView comment_body;
    private Handler mBookCommentHandler;
    
    private String cur_comment_uri;
    private String cur_comment_title;
    
	private final static int DATA_LOAD_ING = 0x001;
	private final static int DATA_LOAD_COMPLETE = 0x002;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_douban_comment_detail);
		
		cur_comment_title 	= getIntent().getStringExtra("comment_title");
		cur_comment_uri 	= getIntent().getStringExtra("comment_uri");
		
    	mBack = (ImageView)findViewById(R.id.book_doubancomment_back);
    	mProgressbar = (ProgressBar)findViewById(R.id.book_doubancomment_head_progress);
    	mRefresh = (ImageView)findViewById(R.id.book_doubancomment_refresh);
    	
    	mLinearlayout = (LinearLayout)findViewById(R.id.book_doubancomment_linearlayout);
     	
    	mBack.setOnClickListener(UIHelper.finish(this));
    	mRefresh.setOnClickListener(refreshClickListener);  

		comment_title = (TextView)findViewById(R.id.douban_comment_detail_title);
		comment_body = (LinkView)findViewById(R.id.douban_comment_detail_body);
		
		mBookCommentHandler = new Handler() {
	
			public void handleMessage(Message msg) {
				
				headButtonSwitch(DATA_LOAD_COMPLETE, 1);
				if (msg.what == 1 && msg.obj != null) {
					MyResult res = (MyResult) msg.obj;
					comment_title.setText( cur_comment_title );
					comment_body.setText( res.getErrorMessage() );
					
				} else if (msg.obj != null) {
					((AppException) msg.obj).makeToast(BookCommentDetail.this);
				}

			}
		};
		
		loadDoubanBookComment(cur_comment_uri, mBookCommentHandler, UIHelper.LISTVIEW_ACTION_REFRESH);

	}
	
    /**
     * 头部加载展示
     * @param type
     * @param action 1-init 2-refresh
     */

    private void headButtonSwitch(int type,int action) {
    	switch (type) {
		case DATA_LOAD_ING:
			if(action==1)mLinearlayout.setVisibility(View.GONE);
			mProgressbar.setVisibility(View.VISIBLE);
			mRefresh.setVisibility(View.GONE);
			break;
		case DATA_LOAD_COMPLETE:
			mLinearlayout.setVisibility(View.VISIBLE);
			mProgressbar.setVisibility(View.GONE);
			mRefresh.setVisibility(View.VISIBLE);
			break;
		}
    }
	

	
	private void loadDoubanBookComment(final String comment_uri, final Handler handler, final int action){  
		
		this.headButtonSwitch(DATA_LOAD_ING, 1);
		new Thread(){
			public void run() {
				
				Message msg = new Message();	
				
				boolean isRefresh = false;
				if(action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;

				try {
					MyResult res = ((AppContext)getApplication()).getDoubanBookComment(comment_uri, isRefresh);
					msg.what = 1;
					msg.obj = res;
				} catch (AppException e) {
	            	e.printStackTrace();
	            	msg.what = -1;
	            	msg.obj = e; 
				}
				
                handler.sendMessage(msg);
			}
			
		}.start();
		
	} 
	
    private View.OnClickListener refreshClickListener = new View.OnClickListener() {
		public void onClick(View v) {	
			loadDoubanBookComment(cur_comment_uri, mBookCommentHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
		}
	};
	

}